#!/usr/bin/env bash

#if [ `uname` != "Darwin" ]; then
#    echo "-- please execute this sh on Mac OS!"
#    exit 0
#fi

#function: create a repository on git server
if [ "x$1" == "xhelp" ] || [ "x$1" == "xh" ]; then
    echo "useage:"
    echo "       $0 h/help                          show this help info"
    echo "       $0 your_git_repository_name    create git repository on remote server"
    exit 0;
fi

#login to remote git host then run command 
#git server info
#host_ip=10.45.66.240
#host_ip=172.16.50.185
#host_ip=192.168.31.8
host_ip=10.8.8.8
user_name=vi
remote_home='$HOME'
html_dir=$remote_home/.git_dir

if [ "$2" == "xc" ];then
     user_name=vi
     host_ip=192.168.1.141
fi



create_remote_repository()
{
    git_repo_name=`basename $1`
    git_addr=git://$host_ip/$git_repo_name
    if [[ $git_repo_name != *.git ]];then
        echo "-- Error:please add a suffix of .git to your git repository name"
        exit 1;
    fi
    #ssh  $user_name@$host_ip  "source $remote_home/.profile && cd $html_dir && git init --bare $1"
    #check if $1 has already exsited in git repository list
    ssh  $user_name@$host_ip  "source $remote_home/.profile && cd $html_dir && ls" | grep $git_repo_name > /dev/null
    #if it already exist
    if [ $? -eq 0 ] ;then
        echo "-- Info:$1 has already exist, when creating on remote git server $git_addr"
    else
        ssh  $user_name@$host_ip  "source $remote_home/.profile && cd $html_dir && git init --bare $git_repo_name"
        echo "-- create git rep $git_repo_name succeed."
    fi
    return 0;
}

if [ $# -eq 1 ] && [ "$1" == "ls" ]; then
    ssh  $user_name@$host_ip  "source $remote_home/.profile && cd $html_dir && ls" 
    exit 0;
fi

if [ $# -ge 1 ]; then
    src_name=`basename $1`
    git_repo_name="${src_name}.git"
    create_remote_repository $git_repo_name
    dir_need_added=$1
    git_addr=git://$host_ip/$git_repo_name

    #check if $1 has already exsited in git repository list
    ssh  $user_name@$host_ip  "source $remote_home/.profile && cd $html_dir && ls" | grep $git_repo_name > /dev/null
    #if it already exist
    if [ $? -eq 0 ] ;then
        if [ -d $dir_need_added ]; then
            if [ -d $dir_need_added/.git ]; then
                echo "-- Info:$1 has already been added in remote $git_addr"
                echo "-- here just push your update to $git_addr"
                cd $dir_need_added
                (
                current_branch=`git branch  | grep "*"| cut -d ' ' -f 2`
                remote_rep=`git config --get branch.${current_branch}.remote`
                git add -A *
                git commit -m"`date` commit update"
                if [ $# -ge 2 ];then 
                    git push -u origin master
                else
                    git push -u $remote_rep $current_branch
                fi
                )
                exit 1;
            else
                cd $dir_need_added
                (
                git init 
                git remote add origin $git_addr
                git add *
                git commit -m"`date` commit firstly"
                if [ $# -ge 2 ];then 
                    git push -u origin master
                else
                    git push -u origin master
                fi
                )
            fi
        else
            echo "-- Error:$1 does exist in local disk"
        fi
    else
        echo "-- Error:$1 does exist in remote $git_addr,please create firstly"
    fi
fi

if [ $# -eq 2 ] && [ "$1" == "clone" ]; then
    src_name_2=`basename $2`

    if [[ $src_name_2 != *.git ]];then
        my_git_repo_name="${src_name_2}.git"
    else
        my_git_repo_name="${src_name_2}"
    fi
    git_addr=git://$host_ip/$my_git_repo_name
    git clone $git_addr
fi

